package com.drinks.yiniang.ui.activity.community;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.community.ReportUserAdapter;
import com.drinks.yiniang.common.recycler.decoration.FirstItemDecoration;
import com.drinks.yiniang.common.recycler.decoration.ItemSpacesDecoration;
import com.drinks.yiniang.entity.common.IdNameListJson;
import com.drinks.yiniang.io.api.CommunityApiIO;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;

/**
 * 举报用户
 *
 * @author han
 * @date 2019/09/10
 */
public class ReportUserActivity extends BaseTranBarActivity implements OnNavgationBarClickListener, BaseQuickAdapter.OnItemClickListener {

    //nav
    private NavigationBarLayout nav_bar_report_user;
    //举报理由列表
    private BaseRecyclerView rcv_report_user;
    //确认
    private Button btn_report_user_sure;

    private ReportUserAdapter mReportUserAdapter;

    //社区id
    private String mCommunityId;
    //当前选个的举报理由id
    private String mCurrentChooseReasonId;

    @Override
    protected void initView() {
        nav_bar_report_user = findViewById(R.id.nav_bar_report_user);

        rcv_report_user = findViewById(R.id.rcv_report_user);

        btn_report_user_sure = findViewById(R.id.btn_report_user_sure);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        mCommunityId = getIntent() != null ? getIntent().getStringExtra(Constants.KeyCommunityId) : null;

        initAdapter();
        getReportReasonList();
    }

    private void initAdapter() {
        rcv_report_user.setLayoutManager(new LinearLayoutManager(mContext));
        rcv_report_user.addItemDecoration(FirstItemDecoration.getItemDPDecoration(10));

        mReportUserAdapter = new ReportUserAdapter(mContext, new ArrayList<IdNameListJson.IdNameListData>());
        mReportUserAdapter.setCenterEmpty(getLayoutInflater(), rcv_report_user);
        mReportUserAdapter.bindToRecyclerView(rcv_report_user);
    }

    @Override
    protected void initListener() {
        nav_bar_report_user.setOnNavgationBarClickListener(this);
        btn_report_user_sure.setOnClickListener(this);
        mReportUserAdapter.setOnItemClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        if (v.getId() == R.id.btn_report_user_sure) {
            doSure();
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_report_user;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        IdNameListJson.IdNameListData data = mReportUserAdapter.getItem(position);

        if (data == null) {
            return;
        }

        mReportUserAdapter.setCurrentChoosePos(position);
        mReportUserAdapter.notifyDataSetChanged();

        mCurrentChooseReasonId = data.getId();
    }

    private void doSure() {
        if(StringUtils.isEmpty(mCommunityId)) {
            ToastUtils.showShort(R.string.text_community_not_exist);
            return;
        }

        if (StringUtils.isEmpty(mCurrentChooseReasonId)) {
            ToastUtils.showShort(R.string.text_select_report_reason);
            return;
        }

        showLoadingDialog();

        CommunityApiIO.getInstance().doCommunityReport(mCommunityId, mCurrentChooseReasonId, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                ToastUtils.showShort(R.string.text_report_success);
                finish();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //获取举报原因
    private void getReportReasonList() {
        showLoadingDialog();

        CommunityApiIO.getInstance().getCommunityReportReason(new APIRequestCallback<IdNameListJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(IdNameListJson res) {
                if (mReportUserAdapter == null) {
                    return;
                }

                mReportUserAdapter.getData().clear();
                mReportUserAdapter.addData(res.getData());
                mReportUserAdapter.loadMoreEnd();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mReportUserAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
